package idiet.service;

import idiet.bean.FoodMaterial;
import idiet.bean.FoodMaterialCategory;
import idiet.dao.hibernate.FoodMaterialCategoryDao;
import idiet.dao.hibernate.FoodMaterialDao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("foodMaterialService")
public class FoodMaterialService {
	@Autowired
	FoodMaterialDao foodMaterialDao;
	@Autowired
	FoodMaterialCategoryDao foodMaterialCategoryDao;
	public List<FoodMaterial> getFoodMaterials(String materialName){
		if(materialName!=null)
			return this.foodMaterialDao.getFoodMaterialsByMaterialName(materialName);
		else
			return this.foodMaterialDao.getFoodMaterials();
	}
	public List<FoodMaterial> getFoodMaterials(FoodMaterialCategory category,boolean isParentCategory){
		if(category!=null){
			if(isParentCategory){
				List<FoodMaterialCategory> categories=this.foodMaterialCategoryDao.getChildFoodMaterialCategories(category);
				if(categories!=null){
					return this.foodMaterialDao.getFoodMaterialBySecondaryCategories(categories);
				}else{
					return null;
				}
			}else{
				return this.foodMaterialDao.getFoodMaterialsBySecondaryCategory(category);
			}
		}else{
			return this.foodMaterialDao.getFoodMaterials();
		}
	}
	public List<FoodMaterial> getFoodMaterials(String materialName,FoodMaterialCategory category,boolean isParentCategory){
		if(materialName!=null&&category!=null){
			if(isParentCategory){
				List<FoodMaterialCategory> categories=this.foodMaterialCategoryDao.getChildFoodMaterialCategories(category);
					return this.foodMaterialDao.getFoodMaterialBySecondaryCategories(categories,materialName);
			}else{
				return this.foodMaterialDao.getFoodMaterialsBySecondaryCategory(category,materialName);
			}
		}else if(materialName==null&&category!=null){
			if(isParentCategory){
				List<FoodMaterialCategory> categories=this.foodMaterialCategoryDao.getChildFoodMaterialCategories(category);
					return this.foodMaterialDao.getFoodMaterialBySecondaryCategories(categories);
			}else{
				return this.foodMaterialDao.getFoodMaterialsBySecondaryCategory(category);
			}
		}else{
			return this.getFoodMaterials(materialName);
		}
	}
}
